package mindteck.store;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class OrderDetailDAO extends BaseDAO{
	private final String INSERTORDERDETAIL = "INSERT INTO ORDERDETAIL VALUES (?,?,?,?)";
	private final String GETORDERDETAILBYORDERID = "SELECT * FROM ORDERDETAIL WHERE ORDERID = ?";

	
	public OrderDetailDAO(){
		
	}
	
	public boolean insertOrderDetail(OrderDetail orderDetail){
		boolean t = true;
		Connection connect =null; 
		PreparedStatement statement = null; 
		
		try{
			connect = getPoolOfConnections();
			statement = connect.prepareStatement(INSERTORDERDETAIL);
			
			statement.setInt(1, orderDetail.getOrderId());
			statement.setInt(2,orderDetail.getProductId());
			statement.setInt(3,orderDetail.getQuantity());
			statement.setDouble(3,orderDetail.getSubtotal());
			
		}catch(SQLException ep){
			ep.printStackTrace();
			t = false;
		}finally{
			if(connect != null){
				try {
					connect.close();
				} catch (SQLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}
		return t;
	}
	
	
	public OrderDetail getOrderDetailByOrderId(int orderId){
		OrderDetail orderDetail = null;
		Connection connect =null; 
		PreparedStatement statement = null; 
		
		try{
			connect = getPoolOfConnections();
			statement = connect.prepareStatement(GETORDERDETAILBYORDERID);
			statement .setInt(1, orderId);
			
			ResultSet rs = statement.executeQuery();
			if(rs.next()){
				orderDetail = new OrderDetail();
				orderDetail.setOrderId(orderId);
				orderDetail.setProductId(rs.getInt("productId"));
				orderDetail.setQuantity(rs.getInt("quantity"));
				orderDetail.setSubtotal(rs.getDouble("subtotal"));
			}
		
			
		}catch(SQLException ep){
			ep.printStackTrace();
	
		}finally{
			if(connect != null){
				try {
					connect.close();
				} catch (SQLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}
		return orderDetail;
	}
	
	
}
